package com.demo.camera2;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.graphics.SurfaceTexture;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.TextureView;

import androidx.appcompat.app.AppCompatActivity;

import com.caiguoping.yuv.Test;

import java.nio.ByteBuffer;

public class MainActivity extends AppCompatActivity implements
        PreviewCallback, TextureView.SurfaceTextureListener {

    private static final String TAG = "MainActivity";

    Bitmap cacheBitmap;

    ByteBuffer cacheByteBuffer;

    Rect src, dst;

    private EasyCamera easyCamera;

    SurfaceHolder holder;

    byte[] abgra;

    private Matrix matrix;

    private TextureView textureView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textureView = findViewById(R.id.texture_view);
        textureView.setSurfaceTextureListener(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }

    @Override
    public void onPreviewFrame(byte[] data, int width, int height) {
        if (cacheBitmap == null) {
            abgra = new byte[width * height * 4];
            cacheBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
            cacheByteBuffer = ByteBuffer.wrap(abgra);
        }

        Test.NV21ToABGR(width, height, data, abgra);

        cacheByteBuffer.position(0);
        cacheByteBuffer.put(abgra);
        cacheByteBuffer.position(0);
        cacheBitmap.copyPixelsFromBuffer(cacheByteBuffer);

/*        Canvas canvas = holder.lockCanvas();
        if (canvas != null) {
            int canvasWidth = canvas.getWidth();
            int canvasHeight = canvas.getHeight();
            int bitmapWidth = cacheBitmap.getWidth();
            int bitmapHeight = cacheBitmap.getHeight();

            *//*if (src == null || dst == null) {
                float scale = Math.max(canvasWidth / (float) bitmapWidth, canvasHeight / (float) bitmapHeight);

                int dstRight = (int) (bitmapWidth * scale);
                int dstBottom = (int) (bitmapHeight * scale);
                int dstLeft = canvasWidth - dstRight;
                int dstTop = canvasHeight - dstBottom;

                dst = new Rect(dstLeft, dstTop, dstRight, dstBottom);
                src = new Rect(0, 0, bitmapWidth, bitmapHeight);
            }

            canvas.drawBitmap(cacheBitmap, src, dst, null);*//*

            if (matrix == null) {
                matrix = new Matrix();
                float scale = Math.max(canvasWidth / (float) bitmapWidth, canvasHeight / (float) bitmapHeight);
                matrix.postScale(scale, scale);
            }
            canvas.drawBitmap(cacheBitmap, matrix, null);

            holder.unlockCanvasAndPost(canvas);
        }*/
    }

    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
        surface.setDefaultBufferSize(width, height);

        matrix = new Matrix();

        matrix.preTranslate((width - 1280) / 2f, (height - 720) / 2f);
        matrix.preScale(1280 / (float) width, 720 / (float) height);

        float max = Math.max(width / 1280f, height / 720f);
        matrix.postScale(max, max, width / 2f, height / 2f);

        textureView.setTransform(matrix);

        easyCamera = new EasyCamera();
        easyCamera.setTexture(surface);
        easyCamera.open("USB2.0_CAM1", 1280, 720);
    }

    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {

    }

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
        easyCamera.close();
        return true;
    }

    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture surface) {

    }
}